<?php

/**
 * Framework_Presenter_Smarty_TableBuilder
 *
 * @author Application Support Group
 * @copyright University of Georgia
 * @license http://www.opensource.org/licenses/gpl-license.php
 * @package BAMF
 * @filesource
*/


/**
 * Framework_Presenter_Smarty_TableBuilder
 *
 *
 * @author Application Support Group
 * @package BAMF
 * @link http://smarty.php.net
*/
class Framework_Presenter_Smarty_TableBuilder extends Framework_Presenter_Smarty_Extension {

	public function __construct() {
		parent::__construct() ;
	}

	public static function register( &$smartyObj ) {
		$smartyExtension = new Framework_Presenter_Smarty_TableBuilder() ;
		$smartyObj->register_function( "tableBuilder", array( $smartyExtension, "tableBuilder" )) ;
	}


// build a search bar for the given collection, format parameters, and state

	public function tableBuilder($params, &$smarty) {
		$_output = array() ;
		
		extract($params);
	
		if ( empty($collection) ) {
			$smarty->trigger_error("tableBuilder: missing 'collection' parameter");
			return;
		}

		if ( empty($format) ) {
			$smarty->trigger_error("tableBuilder: missing 'format' parameter");
			return;
		}

		if(empty($get)) {
			$get = "";
		} else {
			$get = "&amp;".$get;
		}
		// and lookupData...is this required?

// We use the fields defined in the collection schema unless they're being overridden by the format array
		
		$fields = count( $format[ 'fields' ]) > 0 ? $format[ 'fields' ] : $collection->_schema[ 'fields' ] ;
		$modulePath = $smarty->get_template_vars( 'modulePath' ) ;
		$itemNumber = $smarty->get_template_vars( 'itemNumber' ) ;

		$basePath = $modulePath ;
		if( !empty( $itemNumber )) {
			$basePath .= '/' . $itemNumber ;
		}

		$numCols = 0 ;

		$mvSep = '<br />' ;
		if( isset( $format[ 'general' ][ 'multi_value_column_separator' ] )) {
			$mvSep = $format[ 'general' ][ 'multi_value_column_separator' ] ;
		}

		$tableClass = 'tbuilder' ;		
		if( isset( $format[ 'general' ][ 'class' ] ) && !empty( $format[ 'general' ][ 'class' ] )) {
			$tableClass = $format[ 'general' ][ 'class' ] ;
		}
		
		$colClass = array() ;

		$_output[] = '
			<div class="' . $tableClass . '">
			<table border="0" cellpadding="2" cellspacing="0" width="100%">
		' ;

// table header, if specified

		if ( isset( $format[ 'general' ][ 'tableHeader' ] ) && !empty ( $format[ 'general' ][ 'tableHeader' ] )) {
			foreach( $fields as $field ) {
				if ( isset( $format[ 'isVisible' ][ $field ] ) && !empty ( $format[ 'isVisible' ][ $field ] )) {
					$numCols++ ;
				}
			}
			
			$_output[] = '
				<tr>
					<td colspan="' . $numCols . '" class="tableHeader">' . $format[ 'general' ][ 'tableHeader' ] . '</td>
				</tr>
			' ;
		}

// column headers

		$_output[] = '<tr>' ;
		
		foreach( $fields as $field ) {
			if ( isset( $format[ 'colHeader' ][ $field ] ) && !empty( $format[ 'colHeader' ][ $field ] )) {
				$colHeaderText = $format[ 'colHeader' ][ $field ] ;
			} else {
				if ( isset( $collection->_schema[ 'field_aliases' ][ $field ] ) && !empty( $collection->_schema[ 'field_aliases' ][ $field ] )) {
					$colHeaderText = $collection->_schema[ 'field_aliases' ][ $field ] ;
				} else {
					$colHeaderText = $field ;
				}
			}

// Set the class. We'll store this in a new array to use for individual cells below.
			if ( isset( $format[ 'isVisible' ][ $field ] ) && !empty( $format[ 'isVisible' ][ $field ] )) {
				$colClass[ $field ] = "left" ;
				if ( isset( $format[ 'classOverride' ][ $field ] ) && !empty( $format[ 'classOverride' ][ $field ] )) {
					$colClass[ $field ] = $format[ 'classOverride' ][ $field ] ;

				} elseif ( isset( $collection->_schema[ 'field_definitions' ][ $field ] ) && !empty( $collection->_schema[ 'field_definitions' ][ $field ] )) {
					if ( ($collection->_schema[ 'field_definitions' ][ $field ] == DB_DATATYPE_INTEGER && $field != $collection->_schema[ 'db_parameters' ][ 'pkfield' ] && !isset( $collection->_schema[ 'pk_foreign_keys' ][ $field ] )) ||
						( ( $collection->_schema[ 'field_definitions' ][ $field ] == DB_DATATYPE_FLOAT || $collection->_schema[ 'field_definitions' ][ $field ] == DB_DATATYPE_CURRENCY ))
						) {
							$colClass[ $field ] = "right" ;	
					}
				}
				
				$_output[] = '<th class="' . $colClass[ $field ] . '" scope="col">' ;
				
				if ( isset( $format[ 'isCheckbox' ][ $field ] ) && $format[ 'isCheckbox' ][ $field ] ) {
					$_output[] = '<input type="checkbox" name=" ' . $field . '_head" onclick="setAllCheckboxes(\''. $field . '\', this.checked, this.form);" />';
				} else {
					if ( isset( $format[ 'isSortable' ][ $field ] ) && $format[ 'isSortable' ][ $field ] ) {
						$_output[] = '<a href="' . $basePath . '?' . TBUILDER_KEY_SORT . '=' . $field . $get . '">' . $colHeaderText . '</a>' ;
					} else {
						$_output[] = $colHeaderText ;
					}
				}
				
				$_output[] = '</th>' ;
			
			}
		}
		
		$_output[] = '</tr>' ;

// table data

		$counter = 0 ;
		
		foreach ( $collection as $record ) {
			$_row = array() ;
			$counter++ ;
			$pkValue = $record->{$collection->_schema[ 'db_parameters' ][ 'pkfield' ]} ;
			
			// alternate between "zebra0" and "zebra1"
			if ( isset( $format[ 'general' ][ 'zebraStriping' ] ) && $format[ 'general' ][ 'zebraStriping' ] ) {
				$_row[] = '<tr class="zebra' . ($counter % 2) . '">' ;
			} else {
				$_row[] = '<tr>' ;
			}
			
			foreach ( $fields as $field ) {
				$cellData = "" ;
				if ( isset( $format[ 'isVisible' ][ $field ] ) && $format[ 'isVisible' ][ $field ] ) {
					if ( isset( $format[ 'isCheckbox' ][ $field ] ) && $format[ 'isCheckbox' ][ $field ] ) {
						$_row[] = '<td class="center"><input type="checkbox" name="' . $field . '[' . $pkValue . ']" value="1" /></td>' ;

					} else {
						if ( isset( $format[ 'contentOverride' ][ $field ] ) && !empty( $format[ 'contentOverride' ][ $field ] )) {
							$cellData = $format[ 'contentOverride' ][ $field ] ;

						} elseif ( isset( $format[ 'methodResult' ][ $field ] ) && !empty( $format[ 'methodResult' ][ $field ] )) {
							$cellData = $record->{$format[ 'methodResult' ][ $field ]} ;

						} else {
							if ( isset( $collection->_schema[ 'field_definitions' ][ $field ] ) && !empty( $collection->_schema[ 'field_definitions' ][ $field ] )) {
								if ( $collection->_schema[ 'field_definitions' ][ $field ] == DB_DATATYPE_DATETIME && $record->$field) {
									if ( isset( $format[ 'dateFormat' ][ $field ] ) && !empty( $format[ 'dateFormat' ][ $field ] )) {
										$cellData = date( $format[ 'dateFormat' ][ $field ], strtotime( $record->$field )) ;

									} else {
										$cellData = date( "b d, Y I:M p", strtotime( $record->$field )) ;

									}
								} elseif ( $collection->_schema[ 'field_definitions' ][ $field ] == DB_DATATYPE_CURRENCY ) {
									$cellData = sprintf( "$%.2f", $record->$field ) ;

								} elseif ( $collection->_schema[ 'field_definitions' ][ $field ] == DB_DATATYPE_VIRTUAL ) {
									if( is_array( $record->$field ) ) {
										$cellData = implode( $mvSep, $record->$field ) ;
									} else {
										$cellData = $record->$field ;
									}

								} elseif ( isset( $format[ 'customLookup' ][ $field ] ) && !empty( $format[ 'customLookup' ][ $field ] )) {
									$cellData = $format[ 'customLookup' ][ $field ][ $record->$field ] ;

								} elseif ( isset( $collection->_schema[ 'pk_foreign_keys' ][ $field ] ) && !empty( $collection->_schema[ 'pk_foreign_keys' ][ $field ] )) {
									$cellData = $lookupData[ $field ][ $record->$field ] ;

								} else {
									$cellData = $record->$field ;

								}
							} else {

								// this is to catch fields that might not be defined in the .ini
								
								if ( property_exists( $record, $field )) {
									if( is_array( $record->$field ) ) {
										$cellData = implode( $mvSep, $record->$field ) ;
									} else {
										$cellData = $record->$field ;
									}
								}
							}
						}
						
						// Apply any additional text modifications specified in the regex, stringFormat, and truncate arrays in THAT ORDER

						if ( isset( $format[ 'regex' ][ $field ] ) && !empty( $format[ 'regex' ][ $field ] )) {
							$cellData = preg_replace( $format[ 'regex' ][ $field ][ 'srch' ], $format[ 'regex' ][ $field ][ 'rep' ], $cellData ) ;
						}	
						if ( isset( $format[ 'stringFormat' ][ $field ] ) && !empty( $format[ 'stringFormat' ][ $field ] )) {
							$cellData = sprintf( $format[ 'stringFormat' ][ $field ] , $cellData ) ;
						}
						if ( isset( $format[ 'truncate' ][ $field ] ) && !empty( $format[ 'truncate' ][ $field ] )) {
							//Only truncate if  the string is actually too long.
							if($cellData && (strlen($cellData) > $format['truncate'][$field]) ) {
								$cellData = substr( $cellData, 0, $format[ 'truncate' ][ $field ] - 3 ) . '...' ;
							}
						}
						
						$_row[] = '<td class="' . $colClass[ $field ] . '">' ;
						
						if ( isset( $format[ 'fieldHrefUrl' ][ $field ] ) && $format[ 'fieldHrefUrl' ][ $field ] == "mailto" ) {
							$_row[] = '<a href="mailto:' . $cellData . '">' . $cellData . '</a>' ;
						} elseif ( isset( $format[ 'fieldHrefUrl' ][ $field ] ) && !empty( $format[ 'fieldHrefUrl' ][ $field ] )) {

							if ( is_array( $format[ 'fieldHrefUrl' ][ $field ] )) {
								$_links = array() ;
								foreach( $format[ 'fieldHrefUrl' ][ $field ] as $hrefArr ) {
									if ( isset( $hrefArr[ 'suppressField' ] ) && isset( $collection->_schema[ 'field_definitions' ][$hrefArr[ 'suppressField' ]]) && strval( $record->{$hrefArr[ 'suppressField' ]} ) == strval( $hrefArr[ 'suppressValue' ] )) {
										$_links[] = '<span class="suppressedLink">' . $hrefArr[ 'label' ] . '</span>' ;
									} else {
										$onclick = '' ;
										if ( isset( $hrefArr[ 'onclick' ] ) && !empty( $hrefArr[ 'onclick' ] )) {
											$onclick = ' onclick="'.$hrefArr['onclick'] .'"' ;
										}
										
										$_links[] = '<a href="' . $hrefArr[ 'href' ] . '"' . $onclick .'>' . $hrefArr[ 'label' ] . '</a>' ;
									}

								}
								$_row[] = implode( "&nbsp;| ", $_links ) ;
							} else {
								$_row[] = '<a href="'. $format[ 'fieldHrefUrl' ][ $field ] . '">' . $cellData . '</a>' ;
							}

						} else {
							$_row[] = $cellData ;
						}
						
						//Close our column
						$_row[] = '</td>';
						
					}
				}
			}
		

		
			$_row[] = '</tr>' ;

			// We want the replacement loop to be flexible enough to hit any possible
			// property while still catching things that don't exist in the node, so
			// we'll merge the two possible array sources and loop over that. 
			
			$replacementFields = array_unique( array_merge( $fields, $collection->_schema[ 'fields' ] )) ;
	
 			foreach ( $replacementFields as $repField ) {
				if ( property_exists( $record, $repField ) || (isset( $collection->_schema[ 'field_definitions' ][ $repField ] ) && !empty( $collection->_schema[ 'field_definitions' ][ $repField ]))) {
					$_row = str_replace( "%$repField%", $record->$repField, $_row ) ;
				} 
				$_row = str_replace( "%pkValue%", $pkValue, $_row ) ;
			}
			
			$_output[] = implode( "\n", $_row ) ;


		}

		$_output[] = '</table></div>' ;

		return implode( "\n", $_output ) ;


	}

	public function __destruct() {

	}


}
?>